USE [WarehouseManagement]
GO

/****** Object:  StoredProcedure [dbo].[DeleteOrder]    Script Date: 04/27/2011 09:24:41 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		duylv
-- Create date: 
-- Description:	
-- =============================================
Create PROCEDURE [dbo].[DO_DeleteOrder] 
	-- Add the parameters for the stored procedure here
	@OrderID int = 0,
	@IsMaterial int = 0 OUTPUT,
	@IsImport int = 0 OUTPUT,
	@isDeleted int = 0 OUTPUT
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
    -- Insert statements for procedure here
	BEGIN TRANSACTION
		IF EXISTS (SELECT 1 FROM [Order] WHERE OrderID = @OrderID AND IsDelete = 0)
			BEGIN
				SET @isDeleted = 0
			END
			
		ELSE 
			BEGIN
				SET @isDeleted = 1
			END
		UPDATE	[Order]
		SET		IsDelete = 1
		WHERE	[Order].OrderID = @OrderID AND [Order].IsDelete = 0
		SAVE TRANSACTION SaDeleteOrder
		IF @@ERROR <> 0 ROLLBACK TRANSACTION SaDeleteOrder
		UPDATE	MaterialLoaf
		SET		IsDelete = 1
		WHERE	MaterialLoaf.OrderID = @OrderID AND MaterialLoaf.IsDelete = 0
		IF @@ERROR <> 0 ROLLBACK TRANSACTION SaDeleteOrder
		UPDATE	ProductLoaf
		SET		IsDelete = 1
		WHERE	ProductLoaf.OrderID = @OrderID AND ProductLoaf.IsDelete = 0
		IF @@ERROR <> 0 ROLLBACK TRANSACTION SaDeleteOrder
		IF EXISTS (SELECT 1 FROM MaterialLoaf WHERE OrderID = @OrderID)
			BEGIN
				SET @IsMaterial = 1
			END
			
		ELSE 
			BEGIN
				SET @IsMaterial = 0
			END
		SET @IsImport = (SELECT IsImport FROM [Order] WHERE OrderID = @OrderID)
		
	COMMIT TRANSACTION
END

GO


